package cn.spike.order.service.impl;

import cn.spike.basic.util.LoginContext;
import cn.spike.basic.util.PageList;
import cn.spike.order.domain.OrderPetAcquisition;
import cn.spike.order.dto.OrderDto;
import cn.spike.order.mapper.OrderPetAcquisitionMapper;
import cn.spike.order.query.OrderPetAcquisitionQuery;
import cn.spike.order.service.IOrderPetAcquisitionService;
import cn.spike.basic.service.impl.BaseServiceImpl;
import cn.spike.org.domain.Employee;
import cn.spike.org.domain.Shop;
import cn.spike.org.mapper.EmployeeMapper;
import cn.spike.org.mapper.ShopMapper;
import cn.spike.pet.domain.Pet;
import cn.spike.pet.domain.SearchMasterMsg;
import cn.spike.pet.mapper.PetMapper;
import cn.spike.pet.mapper.SearchMasterMsgMapper;
import cn.spike.user.domain.Logininfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderPetAcquisition> pageQuery(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //通过登陆人id找到登陆人的employee信息
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        //如果有shopId说明是店铺管理员或店员
        if(employee.getShop_id()!=null){
            Shop shop = shopMapper.loadById(employee.getShop_id());
            //如果登陆人员工ID不等于店铺的管理员id - 说明是店员 - 只能查看自己店里自己的订单
            if(shop.getAdminId() != employee.getId()){
                query.setEmpId(employee.getId());
            }
            //只能查看自己店铺的订单
            query.setShopId(employee.getShop_id());
        }
        return super.pageQuery(query);
    }

    /**
     * 取消订单
     * @param id - 订单id
     */
    @Override
    @Transactional
    public void cancel(Long id) {
        //将寻主消息放入寻主池 shopId置空 state=3
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        //将订单状态设置为取消 state = -1
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
    }

    /**
     * 确认订单
     * @param orderDto
     */
    @Override
    public void confirm(OrderDto orderDto) {
        Long orderId = orderDto.getId();        //收购订单id
        Integer payType = orderDto.getPayType();//支付方式
        BigDecimal money = orderDto.getMoney(); //成交价 - 成本价
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderId);
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        //确认订单 录入宠物对象
        Pet pet = creatPet(msg,money);
        petMapper.add(pet);

        //修改寻主消息 状态完成
        msg.setState(2);
        searchMasterMsgMapper.update(msg);

        //修改订单信息 - 状态完成 价格为成交价 设置支付方式
        order.setState(1);
        order.setPaytype(payType);
        order.setPetId(pet.getId());
        order.setPrice(money);
        orderPetAcquisitionMapper.update(order);
    }

    private Pet creatPet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        //成本价
        pet.setCostprice(money);
        //默认销售价
        pet.setSaleprice(money);
        //宠物类型
        pet.setTypeId(msg.getPetType());
        //宠物名字
        pet.setName(msg.getName());
        //宠物图片
        pet.setResources(msg.getResources());
        //店铺id
        pet.setShopId(msg.getShopId());
        //寻主消息ID
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }
}
